package com.xjgzinfo.usdp.service.ext;

import com.xjgzinfo.usdp.domain.ImgAssets;
import com.xjgzinfo.usdp.domain.ImgAssets_;
import com.xjgzinfo.usdp.jpa.service.QueryService;
import com.xjgzinfo.usdp.repository.ImgAssetsRepository;
import com.xjgzinfo.usdp.service.dto.ImgAssetsCriteria;
import com.xjgzinfo.usdp.service.dto.ImgAssetsDTO;
import com.xjgzinfo.usdp.service.mapper.ImgAssetsMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service for executing complex queries for {@link ImgAssets} entities in the database.
 * The main input is a {@link ImgAssetsCriteria} which gets converted to {@link Specification},
 * in a way that all the filters must apply.
 * It returns a {@link List} of {@link ImgAssetsDTO} or a {@link Page} of {@link ImgAssetsDTO} which fulfills the criteria.
 */
@Service
@Transactional(readOnly = true)
public class ImgAssetsExtQueryService extends QueryService<ImgAssets> {

    private final Logger log = LoggerFactory.getLogger(ImgAssetsExtQueryService.class);

    private final ImgAssetsRepository imgAssetsRepository;

    private final ImgAssetsMapper imgAssetsMapper;

    public ImgAssetsExtQueryService(ImgAssetsRepository imgAssetsRepository, ImgAssetsMapper imgAssetsMapper) {
        this.imgAssetsRepository = imgAssetsRepository;
        this.imgAssetsMapper = imgAssetsMapper;
    }

    /**
     * Return a {@link List} of {@link ImgAssetsDTO} which matches the criteria from the database.
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public List<ImgAssetsDTO> findByOrCriteria(ImgAssetsCriteria criteria, ImgAssetsCriteria orCriteria) {
        log.debug("find by criteria : {}", criteria);
        Specification<ImgAssets> specification = createSpecification(criteria);
        return imgAssetsMapper.toDto(imgAssetsRepository.findAll(specification.or(createSpecification(orCriteria))));
    }

    /**
     * Return a {@link List} of {@link ImgAssetsDTO} which matches the criteria from the database.
     *
     * @param criteria   The object which holds all the filters, which the entities should match.
     * @param orCriteria The object which holds all the filters, which the entities should match.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public Page<ImgAssetsDTO> findAllByOrCriteria(ImgAssetsCriteria criteria, ImgAssetsCriteria orCriteria, Pageable page) {
        Specification<ImgAssets> specification = createSpecification(criteria);
        return imgAssetsRepository.findAll(specification.or(createSpecification(orCriteria)), page)
                .map(imgAssetsMapper::toDto);
    }

    /**
     * Function to convert {@link ImgAssetsCriteria} to a {@link Specification}
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching {@link Specification} of the entity.
     */
    protected Specification<ImgAssets> createSpecification(ImgAssetsCriteria criteria) {
        Specification<ImgAssets> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getId(), ImgAssets_.id));
            }
            if (criteria.getName() != null) {
                specification = specification.and(buildStringSpecification(criteria.getName(), ImgAssets_.name));
            }
            if (criteria.getType() != null) {
                specification = specification.and(buildStringSpecification(criteria.getType(), ImgAssets_.type));
            }
            if (criteria.getCreateUser() != null) {
                specification = specification.and(buildStringSpecification(criteria.getCreateUser(), ImgAssets_.createUser));
            }
            if (criteria.getCreateDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getCreateDate(), ImgAssets_.createDate));
            }
            if (criteria.getIsPublic() != null) {
                specification = specification.and(buildSpecification(criteria.getIsPublic(), ImgAssets_.isPublic));
            }
            if (criteria.getUserId() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getUserId(), ImgAssets_.userId));
            }
            if (criteria.getIsDeleted() != null) {
                specification = specification.and(buildSpecification(criteria.getIsDeleted(), ImgAssets_.isDeleted));
            }
        }
        return specification;
    }
}